//
//  JSWebController.m
//  JSWebDemo
//
//  Created by ac-hu on 2018/7/6.
//  Copyright © 2018年 ac-hu. All rights reserved.
//

#import "JSWebController.h"

#import "JSWebConfiguration.h"
#import "JSWebControllerManager.h"
#import "NSString+JSWeb.h"
#import "JSWebWKWebViewAdapter.h"
#import "JSWebCallbackManager.h"
#import "JSWebCompatibilityMacros.h"
#import "JSWebMethodManager.h"

@interface JSWebController ()
@property(nonatomic,copy)NSString *urlStr;
@property(nonatomic,strong)JSWebWKWebViewAdapter *wkAdapter;
@property(nonatomic,strong)JSWebConfiguration *configuration;
@end

@implementation JSWebController

-(void)reloadUrl{
    [self.wkAdapter reloadUrl];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = self.navHidden;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    必须移除，不然界面不会被销毁
    NSLog(@"aaaaaaa%d",[self.navigationController.viewControllers containsObject:self]);
    if (![self.navigationController.viewControllers containsObject:self]) {
        [self removeConfig];
    }
}

-(instancetype)initWithUrlStr:(NSString *)UrlStr{
    if (self = [super init]) {
        //这里提前写好加入的网页名-并在方法处理里面配置
//        [JSWebMethodManager addMessageName:@"back"];
        self.urlStr = UrlStr;
        [self addJSWeb];
//        self.identifier = [NSString createOnlyIdentifier];
        [JSWebControllerManager addContrllor:self];
        [self systemConfig];
    }
    return self;
}

-(void)addJSWeb{
    self.configuration = [[JSWebConfiguration alloc]initWithUserIdentifier:@"2343"];
    self.wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height-20) configuration:self.configuration];
    self.wkWebView.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1.0];
    self.wkAdapter = [[JSWebWKWebViewAdapter alloc]initWithWKWebView:self.wkWebView];
    self.wkAdapter.viewController = self;
    self.wkAdapter.urlStr = _urlStr;
    [self.view addSubview:self.wkWebView];
    
    self.view.userInteractionEnabled = YES;
}

-(void)systemConfig{
    //为了兼容ios9以下版本，修改全局userAgent
    __weak typeof(self) weekSelf = self;
    [JSWebCallbackManager callBackStr:@"navigator.userAgent" backSuccessBlock:^(id  _Nullable result) {
        NSString *userAgent = result;
        NSString *newUserAgent = [userAgent stringByAppendingString:@"ios_application"];
        //NSString *newUserAgent = @"1";
        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil];
        [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
        [[NSUserDefaults standardUserDefaults] synchronize];
        if (@available(iOS 9.0, *)) {
            [weekSelf.wkWebView setCustomUserAgent:newUserAgent];
        } else {
            // Fallback on earlier versions
        }
    }];
//    if (JSWebSystemVersionVersion >= 11.0) {
//        self.wkWebView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//    }else{
//        JSWebWarnDeprecatedStart
//        self.automaticallyAdjustsScrollViewInsets = NO;
//        JSWebWarnDeprecatedEnd
//    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.wkAdapter loadUrl];
}

-(void)loadWebURL{
    [self.wkAdapter loadUrl];
}

#pragma mark - 移除配置信息（否则会造成循环引用，相关类无法释放）
-(void)removeConfig{
    [JSWebControllerManager remove:self];
    [self.configuration removeConfig];
    [self.wkAdapter removeConfig];
    self.wkWebView = nil;
    //这里必须设置，造成了循环引用
    self.wkAdapter = nil;
    self.configuration = nil;
}

-(void)dealloc{
    NSLog(@"JSWebController界面被销毁");
}

@end
